package com.atguigu.springbootdemo.controller;



import com.atguigu.springbootdemo.bean.Employee;
import com.atguigu.springbootdemo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EmployeeController {

    /*
        1、创建对象 2、使用 = 赋值
        EmployeeService service = new EmployeeServiceImpl();

        @Autowired:自动感知
            从容器中，感知当前这个变量的类型，自动感知一个适合的类型，找到就赋值。
     */

    @Autowired
    private EmployeeService service;

    /*
    违反了分层解耦的规范
        这里的属性类型，只能使用接口类型

        使用了实现类的类型，不利于后期升级维护
     */
//    @Autowired
//    private EmployeeServiceImpl service;

    //处理用户对单个员工的增删改查
    @RequestMapping("/emp")
    public Object handle(String op, Integer id, String lastname,String gender,String email){
        //先把用户的参数封装为数据模型
        Employee employee = new Employee(id, lastname, gender, email);

        //调用Service业务模型处理
        switch (op){
            //isBlank(str):str是null 或 '' 或 '白字符（tab、回车、空格）'，返回true
            case "select":if (id == null){
                return "id非法，请传入id!";
            }else {
                Employee e = service.getEmployeeById(id);
                return e == null ? "查无此人" : e;
            }

            case "delete":if (id == null){
                return "id非法，请传入id!";
            }else {
                service.deleteEmployeeById(id);
                return "ok";
            }

            case "update":if (id == null){
                return "id非法，请传入id!";
            }else {
                service.updateEmployee(employee);
                return "ok";
            }

            case "insert":{
                service.insertEmployee(employee);
                return "ok";
            }
        }
        return "ok";
    }


    @RequestMapping("/getAllEmp")
    public Object handle1(){
        return service.getAll();
    }

    @RequestMapping("/getAllRegion")
    public Object handle2(){
        return service.getAllRegion();
    }
}


//@RestController
//public class EmployeeController {
//    private EmployeeService service;
//
//    //处理单个员工的增删改查
//    @RequestMapping("/emp")
//    public Object handle(String op,Integer id,String lastname,String gender,String email){
//        //先把用户的参数封装为数据模型
//        Employee employee = new Employee(id, lastname, gender, email);
//
//        //调用service业务模型处理
//        switch (op){
//            case "select":if (id == null){
//                return "id非法，请传入id!";
//            }else {
//                Employee e = service.getEmployeeById(id);
//                return e == null ? "查无此人" : e;
//            }
//
//            case "delete":if (id == null){
//                return "id非法，请传入id!";
//            }else {
//                service.deleteEmployeeById(id);
//                return "ok";
//            }
//
//            case "update":if (id == null){
//                return "id非法，请传入id!";
//            }else {
//                service.updateEmployee(employee);
//                return "ok";
//            }
//
//            case "insert":{
//                service.insertEmployee(employee);
//                return "ok";
//            }
//        }
//        return "ok";
//    }
//
//    @RequestMapping("/getAllEmp")
//    public Object handle1(){
//        return service.getAll();
//    }
//}


//@RestController
//public class EmployeeController {
//    private EmployeeService service;
//    //处理单个员工的增删改查
//    public Object handle(String op,Integer id,String lastname,String gender,String email){
//        //先把用户的参数封装为数据模型
//        Employee employee = new Employee(id, lastname, gender, email);
//
//
//        //调用service业务模型处理
//        switch (op){
//            case "select":if (id == null){
//               return "id非法，请传入id!";
//            }else {
//                Employee e = service.getEmployeeById(id);
//                return e == null ? "查无此人" : e;
//            }
//
//            case "delete":if (id == null){
//                return "id非法，请传入id!";
//            }else {
//                service.deleteEmployeeById(id);
//                return "ok";
//            }
//
//            case "update":if (id == null){
//                return "id非法，请传入id!";
//            }else {
//                service.updateEmployee(employee);
//                return "ok";
//            }
//
//            case "insert":{
//                service.insertEmployee(employee);
//                return "ok";
//            }
//        }
//        return "ok";
//    }
//    @RequestMapping("/getAllEmp")
//    public Object handle1(){
//        List<Employee> all = service.getAll();
//        return all;
//    }
//}